-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathConnect-SSH.ps1
106 lines (82 loc) · 3.01 KB
/
Connect-SSH.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<#
$Metadata = @{
Title = "Connect SSH Session"
Filename = "Connect-SSH.ps1"
Description = ""
Tags = "powershell, remote, session, ssh"
Project = ""
Author = "Janik von Rotz"
AuthorContact = "http://janikvonrotz.ch"
CreateDate = "2013-05-17"
LastEditDate = "2014-03-06"
Version = "3.1.2"
License = @'
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
'@
}
#>
function Connect-SSH{
<#
.SYNOPSIS
Remote management for ssh sessions.
.DESCRIPTION
Starts a ssh session with the parameters from the remote config file.
.PARAMETER Name
Server names from the remote config file.
.PARAMETER User
Username (overwrites remote config file parameter).
.PARAMETER Port
Port (overwrites remote config file parameter).
.PARAMETER PrivatKey
PrivatKey (overwrites remote config file parameter).
.EXAMPLE
Connect-SSH -Name firewall
#>
#--------------------------------------------------#
# Parameter
#--------------------------------------------------#
param (
[parameter(Mandatory=$true)]
[string[]]$Name,
[parameter(Mandatory=$false)]
[string]$User,
[parameter(Mandatory=$false)]
[int]$Port,
[parameter(Mandatory=$false)]
[string]$PrivatKey
)
#--------------------------------------------------#
# main
#--------------------------------------------------#
if (Get-Command "putty"){
# Load Configurations
$Servers = Get-RemoteConnection -Name $Name
if(!(Get-ChildItem -Path $PSconfigs.Path -Filter $PStemplates.WinSCP.Name -Recurse)){
Write-Host "Copy $($PStemplates.WinSCP.Name) file to the config folder"
Copy-Item -Path $PStemplates.WinSCP.FullName -Destination (Join-Path -Path $PSconfigs.Path -ChildPath $PStemplates.WinSCP.Name)
}
$IniFile = $(Get-ChildItem -Path $PSconfigs.Path -Filter $PStemplates.WinSCP.Name -Recurse).FullName
$Servers | %{
# get port from Protocol
if(!$Port){
$_.Protocol | %{if($_.Name -eq "ssh" -and $_.Port -ne ""){$Port = $_.Port}}
}
if(!$Port -or $Port -eq 0){
$Port = 22
}
# set servername
$Servername = $_.Name
# set username
if(!$User){$User = $_.User}
# set privatkey
if(!$PrivatKey){$PrivatKey = Invoke-Expression ($Command = '"' + $_.PrivatKey + '"')}
if($PrivatKey -eq ""){
Invoke-Expression "putty $User@$Servername -P $Port -ssh"
}else{
Invoke-Expression "putty $User@$Servername -P $Port -ssh -i '$PrivatKey'"
}
}
}
}